
import AutoOrderHistoryEntity from "./AutoOrderHistory.entity";
import pagination from "ROOT/common/pagination";
import { Pagination } from "ROOT/interface/List";
import { autoToFixed } from "ROOT/utils";
import { MysqlDataSource } from "ROOT/db/orm";

// export class AutoOrderHistoryService extends CrudService<AutoOrderHistory>{

// }
const repository = MysqlDataSource.getRepository(AutoOrderHistoryEntity)
/**
 * 查询
 * @param {object} query
 */
export const find = async function(query: Partial<AutoOrderHistoryEntity> = {}, paginationOption?: Pagination) {
    const {skip, take, current} = pagination(paginationOption);
    const [list, total] = await repository
    .createQueryBuilder()
    .where(query)
    .orderBy('datetime', 'DESC')
    .skip(skip)
    .take(take)
    .getManyAndCount();

    return {
        list: list.map((item) => {
            return {
                ...item,
            };
        }),
        pagination: {
            current,
            pageSize: take,
            total,
        }
    };
}

/**
 * 单个
 * @param {object} query
 * @param { Document }
 */
 export const findOne = function(query: Partial<AutoOrderHistoryEntity>) {
    return repository.findOne({where: query});
}

/**
 * 更新单个
 * @param {object} query
 * @param { Document }
 */
export const updateOne = function(query: Partial<AutoOrderHistoryEntity>, newData: Partial<AutoOrderHistoryEntity>) {
    return repository.update(query, newData);
}

/**
 * 新增
 * @param {object} query
 * @param { Document }
 */
export const create = function(data: Partial<AutoOrderHistoryEntity>) {
    data.amount = autoToFixed(data.amount);
    data.price = autoToFixed(data.price);
    const autoOrderHistoryEntity = new AutoOrderHistoryEntity();
    Object.assign(autoOrderHistoryEntity, data);
    return repository.save(autoOrderHistoryEntity);
}
